package com.itstudio.portal.controller;


import com.itstudio.portal.entity.RestResponse;
import com.itstudio.portal.exception.SystemException;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionController {

    @ExceptionHandler(SystemException.class)
    public RestResponse handleException(SystemException e){
        return RestResponse.failure(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = {AccessDeniedException.class, AuthenticationException.class})
    public ResponseEntity<String> handleSecurityExceptions(Exception ex) throws Exception {
        // 不处理这些异常，让Spring Security接手
        throw ex;
    }

    @ExceptionHandler(Exception.class)
    public RestResponse handleException(Exception e){
        return RestResponse.failure(500, e.getMessage());
    }
}
